import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';

import '../util/util.dart';

part 'sql.g.dart';

@JsonSerializable()
class TranslateBean {
  static final String tableName = "translate";
  static final String columnDmsm = "dmsm";
  static final String columnDmz = "dmz";
  static final String columnDmlb = "dmlb";
  static final String databaseName = "translate.db";

  String dmz;
  String dmsm;
  String dmlb; //作为httpOrHttps使用时，mc存的时http或者https，dm存的是0或1

  TranslateBean(this.dmlb, {this.dmz, this.dmsm});

  factory TranslateBean.json2Bean(String result) =>
      _$TranslateBeanFromJson(json.decode(result));

  factory TranslateBean.fromJson(Map<String, dynamic> json) =>
      _$TranslateBeanFromJson(json);

  static Map<String, dynamic> bean2Json(TranslateBean instance) =>
      _$TranslateBeanToJson(instance);

  static List<TranslateBean> json2List(String result) {
    List array = json.decode(result);
    List<TranslateBean> _beanList =
        array.map((map) => TranslateBean.fromJson(map)).toList();
    return _beanList;
  }

  static String getValueByDmmcAndKey(
      Map<String, List<Map>> mapList, String dmlb, String dmz, bool loop) {
    if (TextUtils.isEmpty(dmlb)) {
      return '';
    } else {
      if (loop) {
        StringBuffer buffer = StringBuffer();
        mapList[dmlb].forEach((map) {
          dmz.split('').forEach((item) {
            if (null != map[columnDmz] && item == map[columnDmz]) {
              buffer.write(map[columnDmsm]);
              buffer.write(' ');
            }
          });
        });
        return buffer.toString();
      } else {
        StringBuffer buffer = StringBuffer();
        mapList[dmlb].forEach((map) {
          if (null != map[columnDmz] && dmz == map[columnDmz]) {
            buffer.write(map[columnDmsm]);
            buffer.write(' ');
          }
        });
        /*if (dmmc == StrImpl.gcjk) {
          Print.out(buffer.toString(), tag: "getValueByDmmcAndKey");
        }*/
        return buffer.toString();
      }
    }
  }

  @override
  String toString() {
    return '{"dmlb":"$dmlb","dmz":"$dmz","dmsm":"$dmsm"}';
  }
}
